package game.block.command;

import game.block.Constants;
import game.block.entity.Entity;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;

public class RemoveCommand extends Command implements Constants {

    private Entity block;
    private Board gameBoard;
    private boolean removedFromTraps;
    private boolean removedFromBlocks;

    public RemoveCommand(Entity block, Tile tile, Board gameBoard) {
        this.block = block;
        this.gameBoard = gameBoard;
        removedFromTraps = false;
        removedFromBlocks = false;
    }

    @Override
    public void execute() {
        remove(block);
        gameBoard.setBlockSelected(null);
    }

    public void remove(Entity entity) {
        entity.getCurrentTile().remove(entity);
        gameBoard.getEntities().remove(entity);
        gameBoard.removeEntity(entity);
        if (entity instanceof TrapEntity) {
            gameBoard.getTraps().remove(entity);
            removedFromTraps = true;
        }
        if (entity instanceof MovableEntity) {
            gameBoard.getMovableBlocks().remove(entity);
            
            removedFromBlocks = true;
        }
    }

    public void add(Entity entity) {
        entity.getCurrentTile().add(entity);
        gameBoard.getEntities().add(entity);
        if (removedFromTraps) {
            gameBoard.getTraps().add((TrapEntity) entity);
        }
        if (removedFromBlocks) {
            gameBoard.getMovableBlocks().add((MovableEntity) entity);
        }
    }

    @Override
    public void undo() {
        add(block);
    }

}
